var layer;
var form;
var upload;
var iconMark = 0;
var laySP;
$(function(){
	// 显示头像
	$("#index_img").attr("src",$projectPath+loginImg+"?v="+Math.random());
	layui.use(['layer','form','upload'], function() {
		layer = layui.layer;
		form = layui.form;
		upload = layui.upload;
		//上传头像
		upload.render({
		    elem: '#jzjzImg' //绑定元素
		    ,url: '' //上传接口
		    ,accept: 'images'
		    ,exts: 'jpg|png|jpeg'
			,before: function(obj){
				var idcard_temp = $("#basic_form").find("input[name='idcard_temp']").val();
				$("#basic_form").find("input[name='jzjz']").val("/upload/photo/jzjz/"+oid+"/"+idcard_temp+".jpg?v="+Math.random())
				this.url = encodeURI($projectPath+'/user/uploadjzjzImg?idcard='+idcard_temp);
	        }
		    ,done: function(res){
			  	layer.msg(res.msg,{offset:"100px"})
			  	if(res.success){
			  		$("#jzjzImg").attr("src",$projectPath+res.data+"?v="+Math.random());
			  		$("#index_img").attr("src",$projectPath+res.data+"?v="+Math.random());
			  	}
		    }
		    ,error: function(){
		    	 layer.msg("上传接口异常",{offset:"100px"})
		    }
		});
		if(!sp){
			suoping();
		}
		form.on('submit(subSp)',function(data){
			subSP(1);
			return false;
		})
		 //自定义验证规则
		form.verify({
			 pass: [
			     /^[\S]{6,12}$/
			     ,'密码必须6到12位，且不能出现空格'
		     ]
		});
		form.on('submit(submitForm)', function(data) {
			var lay = layer.load(0,{
				shade: [0.2, 'gray']
			});
			$.post($projectPath+'/user/save', data.field, function(res) {
				layer.closeAll();
				layer.msg(res.msg);
			})
			return false;
		})
	});
	$('.layui-layout-right dd').click(function(){
		$(this).removeClass('layui-this');
	})
	$('#selectPass').click(function(){
		if(iconMark == 0){
			iconMark = 1;
			$(this).html('&#xe664;');
			$('#basic_form').find('input[name="loginPassword"]').attr('type','text');
		}else{
			iconMark = 0;
			$(this).html('&#xe6af;');
			$('#basic_form').find('input[name="loginPassword"]').attr('type','password');
		}
	})
	setInterval("getSysDate()",1000);
})


//锁屏
function suoping() {
	subSP();
	$('#suoping').val('');
	laySP = layer.open({
		title : '请输入登录密码解锁',
		type:1,
		skin : 'layui-layer-molv',// 样式类名
		closeBtn : 0,
		area: ['350px','100px'],
		btn : false,
		content:$('#div_sp'),
		time:0
	}, function() {

	});
}
// 锁屏校验
function subSP(mark) {
	var obj = {};
	if(mark){
		var val = $('#suoping').val();
		if(!val){
			return layer.msg("请输入密码",{offset:"25%"});
		}
		obj = {'suoping':aesMinEncrypt(val),'mark':mark}
	}
	$.post($projectPath+'/index/suoping', obj, function(data) {
		if (data.state) {
			layer.close(laySP);
		}
		if(!data.msg){
			return layer.msg("您尚未登录，请刷新页面",{offset:"25%"});
		}
		layer.msg(data.msg,{offset:"25%"});
	})
}
//个人信息
function basic(){
	$.post($projectPath+'/index/basicData', function(res) {
		if (res.success) {
			var data = res.data;
			form.val('basicForm', {
				"jzjz" : data.jzjz,
				"id" : data.id,
				"name" : data.name,
				"loginName" : data.loginName,
				"loginPassword" : data.loginPassword,
				"keyDate_temp" : data.keyDate,
				"birthday_temp" : data.birthday,
				"idcard_temp" : data.idcard,
				"rname_temp" : data.rname,
				"remarks" : data.remarks
			})
			if(data.jzjz){
				$("#jzjzImg").attr('src',$projectPath+data.jzjz+"?v="+Math.random());
			}
			$('#basic_form input[name="sex"][title="男"]').attr("checked",data.sex == '男' ?true:false);
			$('#basic_form input[name="sex"][title="女"]').attr("checked",data.sex == '女' ?true:false);
			form.render('radio');
			layer.open({
				title : '个人基本资料',
				type:1,
				skin : 'layui-layer-molv',// 样式类名
				area: ['400px','600px'],
				btn : false,
				content:$('#basic_form'),
				time:0
			}, function() {
				
			});
		}else{
			layer.msg(data.msg);
		}
	})
}

// 官网
function openUrl() {
	window.open('http://www.geesuntar.com/');
}
//大数据界面
function showData() {
	window.open($projectPath+'/views/data/sysData?oid='+oid);
}
